package com.webbdong.aop.agent.test.bytebuddy;

import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.MethodDelegation;
import net.bytebuddy.implementation.SuperMethodCall;
import net.bytebuddy.matcher.ElementMatchers;
import net.bytebuddy.utility.JavaModule;

import static com.webbdong.aop.agent.test.bytebuddy.BytebuddyTest.internalDelegate;

/**
 * @program: spring-agent
 * @description:
 * @author: zt22328 赵儒定
 * @create: 2023-03-30 10:59
 **/
public class DefaultTransformer implements AgentBuilder.Transformer {

    @Override
    public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule) {
// 方法拦截
        builder.constructor(ElementMatchers.any())
                .intercept(SuperMethodCall.INSTANCE.andThen(
                        MethodDelegation.to(forInternalDelegateClass("com.webbdong.aop.agent.test.ConstructorInterceptor"))));
        return builder;
    }

    public static Class forInternalDelegateClass(String methodsInterceptor) {
        try {
            return Class.forName(internalDelegate(methodsInterceptor));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}
